package com.hurl.waguji.web.admin;

import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Struts2Utils;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class CheckLoginInterceptor implements Interceptor{
	
	@Override
	public void destroy() {
		
	}

	@Override
	public void init() {
		
	}

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		if(isLoginedIn(ai)){
			return ai.invoke();
		}else{
			String requestUrl = Struts2Utils.getRquest().getRequestURL().toString();
			ActionContext.getContext().getSession().put("redirecturl", requestUrl);
			return Action.LOGIN;
		}
	}
	private boolean isLoginedIn(ActionInvocation ai){
		String username = (String)Struts2Utils.getSessionAttributes().get("username");
		return StringUtils.isNotBlank(username);
	}

}
